<?php

namespace Lims\ContractBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

use Utils\SecurityBundle\CustomClass\GenerateCodeFunctions;
use Utils\SecurityBundle\CustomClass\SecurityFunctions;
use Lims\MainBundle\Entity\Contract;

class ContractController extends Controller
{
    public function listAction()
    {
        return $this->render('LimsContractBundle:Contract:list.html.twig', array());
    }
    
    public function saveAjaxAction()
    {
        $contractPopupType = SecurityFunctions::clear(isset($_POST['contractPopupType']) ? $_POST['contractPopupType'] : null);
        $contractSourceRegister = SecurityFunctions::clear(isset($_POST['contractSourceRegister']) ? $_POST['contractSourceRegister'] : null);
        $contractTargetRegister = SecurityFunctions::clear(isset($_POST['contractTargetRegister']) ? $_POST['contractTargetRegister'] : null);
        $contractName = SecurityFunctions::clear(isset($_POST['contractName']) ? $_POST['contractName'] : null);
        $contractSignedDate = SecurityFunctions::clear(isset($_POST['contractSignedDate']) ? $_POST['contractSignedDate'] : null);
        $contractBeginDate = SecurityFunctions::clear(isset($_POST['contractBeginDate']) ? $_POST['contractBeginDate'] : null);
        $contractEndDate = SecurityFunctions::clear(isset($_POST['contractEndDate']) ? $_POST['contractEndDate'] : null);
        $contractDescription = SecurityFunctions::clear(isset($_POST['contractDescription']) ? $_POST['contractDescription'] : null);
        
        $em = $this->get('doctrine')->getManager();
        
        try{            
            $contract = new Contract();
            $contract->setContractTypeId($contractPopupType);
            $contract->setSourceRegisterNumber($contractSourceRegister);
            $contract->setTargetRegisterNumber($contractTargetRegister);
            $contract->setCode(GenerateCodeFunctions::generateCode(5));
            $contract->setName($contractName);
            $contract->setContractSignedDate(new \DateTime(date($contractSignedDate)));
            $contract->setContractBeginDate(new \DateTime(date($contractBeginDate)));
            $contract->setContractEndDate(new \DateTime(date($contractEndDate)));
            $contract->setDescription($contractDescription);
            $contract->setIsActive(1);
            $contract->setCreatedDate(new \DateTime(date('Y-m-d h:i:s')));
            $contract->setCreatedUserId($this->container->get('security.context')->getToken()->getUser()->getId());
            
            $em->persist($contract);
            $em->flush();
            
            $contractList = $em->getRepository('LimsMainBundle:Contract')->findAvailableContracts();
            
            $contractListHtml = "";
            foreach($contractList AS $row)
            {
                $contractListHtml .= "<option value=\"".$row['ID']."\" ";
                    if($row['ID'] == $contract->getId())
                    {
                        $contractListHtml .= " selected=\"selected\" ";
                    }
                $contractListHtml .= ">".$row['NAME']."</option>";
            }
            
            $result = array('status' => 'success', 'msg' => 'Амжилттай нэмлээ!', 'data' => array('id' => $contract->getId(), 'html' => $contractListHtml));
            
        }  catch (\Exception $e)
        {
            $result = array('status' => 'failed', 'msg' => 'Алдаа гарлаа!');
        }
        
        
        $response = new Response(json_encode($result));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
        
        
    }
    
}
